package com.cqs.leetcode.link;

import com.cqs.leetcode.TestCaseUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RemoveDuplicatesfromSortedArrayII80 {

    static class Solution {

        public int removeDuplicates(int[] nums) {
            if (nums.length <= 2) return nums.length;
            int idx = 0, cnt = 1;
            for (int i = 1; i < nums.length; i++) {
                if (nums[i] == nums[idx]) {
                    if (++cnt <= 2) {
                        nums[++idx] = nums[i];
                    }
                } else {
                    nums[++idx] = nums[i];
                    cnt = 1;
                }
            }
            return idx + 1;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        List<int[]> testCase = new ArrayList<>();
        testCase.add(new int[]{0, 0, 1, 1, 1, 1, 2, 3, 3});
        testCase.add(new int[]{1, 1, 1, 1, 2, 2, 3});
        testCase.stream().forEach(it -> {
            System.out.print(TestCaseUtils.arrFormat(it) + "\t");
            int le = solution.removeDuplicates(it);
            System.out.println(TestCaseUtils.arrFormat(Arrays.copyOf(it, le)) + "\t");
        });
    }
}
